#include "discountmanagement.h"
#include "ui_discountmanagement.h"

discountManagement::discountManagement(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::discountManagement)
{
    ui->setupUi(this);

    db = QSqlDatabase::database("conn");//链接数据库
    QSqlQuery query;//创建对象时，系统会自动完成跟数据库的关联
    query.setForwardOnly(true);
    ui->setBtn->setShortcut(Qt::Key_Return);//设置保存快捷键为键盘的“回车”键

    initList();

    //    QString sqlUpdate = QString("update menu set discount = 1");
    //    //    UPDATE table_name
    //    //    SET column1 = value1, column2 = value2...., columnN = valueN
    //    //    WHERE [condition];

    //    if(!query.exec(sqlUpdate)){
    //        qDebug() << "update data error" << db.lastError();
    //    }else{
    //        QMessageBox::information(this, "友情提示", "修改菜品成功");
    //    }
}

discountManagement::~discountManagement()
{
    delete ui;
}

void discountManagement::initList()
{
    ui->discountList->clear();//清空表内容
    QSqlQuery query;
    query.setForwardOnly(true);

    QString sqlSelect = QString ("select * from menu");
    if(!query.exec(sqlSelect)){//查找关键字
        qDebug() << "search data error" << db.lastError();
    }else {
        while(query.next()){
            QString name = query.value("name").toString();
            QString discount = query.value("discount").toString();
            QString item = name + "****折扣为：" + discount;
            ui->discountList->addItem(item);
        }
    }
}

void discountManagement::on_setBtn_clicked()
{
    if(ui->allEdit->text().isEmpty()){
        QMessageBox::information(this, "友情提示", "请输入折扣");
        return;
    }
    QSqlQuery query;
    query.setForwardOnly(true);
    double allDiscount = ui->allEdit->text().toDouble();

    QMessageBox::StandardButton confirm = QMessageBox::Yes;
    confirm = QMessageBox::question(this,"友情提示",QString("是否设置全场折扣为%1").arg(allDiscount));
    if(confirm == QMessageBox::No){
        return;
    }

    QString sqlUpdate = QString("update menu set discount = %1").arg(allDiscount);
    //UPDATE table_name
    //SET column1 = value1, column2 = value2...., columnN = valueN
    //WHERE [condition];
    if(!query.exec(sqlUpdate)){
        qDebug() << "update data error" << db.lastError();
    }else{
        QMessageBox::information(this, "友情提示", QString("成功修改全场折扣为%1").arg(allDiscount));
    }
    initList();
}

void discountManagement::on_discountList_itemClicked(QListWidgetItem *item)
{
    QString singleDiscount = QInputDialog::getText(this, "设置单品折扣", "请输入折扣");//获取单品折扣
    if(singleDiscount.isEmpty()){
        QMessageBox::information(this, "友情提示", "请输入折扣");
        return;
    }
    QString discountName = item->text().split("*",QString::SkipEmptyParts).at(0);
    QSqlQuery query;
    query.setForwardOnly(true);

    QString sqlUpdate = QString("update menu set discount = %1 where name = '%2'")
            .arg(singleDiscount).arg(discountName);
    if(!query.exec(sqlUpdate)){
        qDebug() << "update data error" << db.lastError();
    }else{
        QMessageBox::information(this, "友情提示", QString("成功修改%1的折扣为%2")
                                 .arg(discountName).arg(singleDiscount));
        qDebug() << discountName;
    }
    initList();
}
